--UI窗体基类
module("UI.UIWindow", package.seeall)

UIWindow = {}

--构造函数
function UIWindow:new(o)
    o = o or {};
    setmetatable(o, { __index = self });
    return o;
end

UIWindow.name = nil      --窗体名字
UIWindow.Container = nil --窗体的UIPanel
UIWindow.Root = nil      --窗体的根物体

UIWindow.OnShow = nil    --激活事件
UIWindow.OnClose = nil   --关闭事件
UIWindow.myContainerIsOpend = false
UIWindow.Screen = UnityEngine.Screen
--初始化界面
function UIWindow:Init()
    -- body
end

--打开界面
function UIWindow:Show(openAniType)
    self.Container.gameObject:SetActive(true)
    if not self.OnShow or type(self.OnShow) ~= "function" then
        if nil ~= self.Container then
            if openAniType ~= nil then
                self:WindowsOpenAni(openAniType)
            else
                self.Container.alpha = 1
            end
            
            self.myContainerIsOpend = true
        end
    else
        if nil ~= self.Container then
            if openAniType ~= nil then
                self:WindowsOpenAni(openAniType)
            else
                self.Container.alpha = 1
            end
        end
        self:OnShow()
        if nil ~= self.Container then
            self.myContainerIsOpend = true
        end
    end
end

--关闭界面
function UIWindow:Close(closeAniType)
    if self.myContainerIsOpend then
        self.myContainerIsOpend = false
        if nil ~= self.Container then
            if closeAniType ~= nil then
                if closeAniType==1 then
                local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
                UIWinMgr.aniWin[#UIWinMgr.aniWin]=nil;
                local newGameLoobyWin = UIWinMgr.WinSet["NewGameLoobyWin"]
                --返回用的
                -- if newGameLoobyWin~=nil then
                --      newGameLoobyWin.Container.gameObject:SendMessage("SetLuaData",#UIWinMgr.aniWin)
                -- end
                end
                self:WindowsCloseAni(closeAniType)
            else
               -- self.Container.alpha = 0
                self.Container.gameObject:SetActive(false);
            end
        end
        --self:ResetDepthToZero()
        if not self.OnClose or type(self.OnClose) ~= "function" then
            return
        end
        self:OnClose()
    end
end

function UIWindow:WindowsCloseAni(closeAniType)
    local DOTweenMgr = require("UI.DOTweenMgr")


    self.Container.gameObject.transform.localPosition = Vector3.zero

    if closeAniType == 1 then
        function callback_start()

        end

        function callback_end()
            self.closeTweener = nil
        end

        if self.openTweener then
            self.openTweener.Kill(self.openTweener)
        end
        if self.closeTweener then
            self.closeTweener.Kill(self.closeTweener)
        end

        self.closeTweener = DOTweenMgr.DTDOMove(
            self.Container.gameObject.transform,
            Vector3(900, 0, 0),
            0.5,
            1,
            false,
            callback_start,
            callback_end,
            0,
            3
        )
    end
    if closeAniType == 2 then
        for i = 1, 20 do
            function delayCallback()
                self.Container.alpha = 1 - i / 20
            end

            CountDownMgr.Instance:CreateCountDown(0.01 * i, 0.01 * i, delayCallback)
        end
    end
end

function UIWindow:WindowsOpenAni(openAniType)
    print("___________________")
    local DOTweenMgr = require("UI.DOTweenMgr")
    self.myContainerIsOpend = true
    self.Container.alpha = 0
    self.Container:Refresh()
  
    if openAniType == 1 then
        function delayCallback()
            function callback_end() self.openTweener = nil end
            function callback_start() end

            self.Container.gameObject.transform.localPosition = Vector3(900, 0, 0)
            self.Container:Refresh()
            self.Container.alpha = 1

            if self.openTweener then self.openTweener.Kill(self.openTweener) end
            if self.closeTweener then self.closeTweener.Kill(self.closeTweener) end

            self.openTweener = DOTweenMgr.DTDOMove(self.Container.gameObject.transform, Vector3(0, 0, 0), 0.5,
                1,
                true,
                callback_start,
                callback_end,
                0,
                3)
        end

        CountDownMgr.Instance:CreateCountDown(0, 0, delayCallback)
    end
    if openAniType == 2 then
        for i = 1, 20 do
            function countDownCallBack()
                self.Container.alpha = i / 20
            end
            CountDownMgr.Instance:CreateCountDown(0.01 * i, 0.01 * i, countDownCallBack)
        end
    end
    if openAniType == 3 then
        function delayCallback()
            function callback_end() self.openTweener = nil end
            function callback_start() end

             self.Container.gameObject.transform.localScale = Vector3(0.1, 0.1, 0.1)
            self.Container.alpha = 1
            self.Container:Refresh()

            -- if self.openTweener then self.openTweener.Kill(self.openTweener) end
            -- if self.closeTweener then self.closeTweener.Kill(self.closeTweener) end

            self.openTweener = DOTweenMgr.DTScale(
                self.Container.gameObject.transform, 
                Vector3(1, 1, 1), 
                0.5,
                1,
                callback_end
            )
        end

        CountDownMgr.Instance:CreateCountDown(0, 0, delayCallback)
    end
end

--界面置顶
function UIWindow:BringToTop()
    if not self.Root or not self.Container then
        return
    end

    local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
    local maxDepth = nil
    for k, v in pairs(UIWinMgr.WinSet) do
        local win = v
        if win.Root then
            if win.Container then
                local panels = win.Root:GetComponentsInChildren(typeof(UIPanel), true) --返回UIPanel的数组
                for i = 0, panels.Length - 1 do                                        --遍历数组
                    if not maxDepth or panels[i].depth > maxDepth then
                        maxDepth = panels[i].depth
                    end
                end
            end
        end
    end
    local panels = self.Root:GetComponentsInChildren(typeof(UIPanel), true) --返回UIPanel的数组
    local localMax = 999999999
    for i = 0, panels.Length - 1 do                                         --遍历数组
        if panels[i].depth < localMax then
            localMax = panels[i].depth
        end
    end

    local change = localMax - maxDepth
    if change <= 0 then
        change = maxDepth - localMax + 1
    else
        change = 0
    end

    for i = 0, panels.Length - 1 do
        panels[i].depth = panels[i].depth + change
    end
end

--将界面深度置为0
function UIWindow:ResetDepthToZero()
    if not self.Container then
        return
    end
    if 0 >= self.Container.depth then
        return
    end
    local panels = self.Container.transform:GetComponentsInChildren(typeof(UIPanel), true) --返回子panel
    local down = self.Container.depth
    self.Container.depth = 0
    for i = 0, panels.Length - 1 do --遍历数组
        panels[i].depth = panels[i].depth - down
    end
end

--销毁界面
function UIWindow:Destroy()
    if nil ~= self.Root then
        GameObject = UnityEngine.GameObject
        GameObject.Destroy(self.Root)
    end
end

--创建窗体
function UIWindow:CreateWin(openAniType)
    --加载资源，创建Unity对象
    local GameObject = UnityEngine.GameObject
    local ResMgr = require("Res.ResMgr").ResMgr
    local BehaviourEvent = require("BehaviourEvent").BehaviourEvent

    local root = GameObject(self.name)
    root.transform.parent = UIHelper.Get2DRoot().transform
    root.transform.localScale = Vector3(1, 1, 1);
    root.transform.localPosition = Vector3(0, 0, 0);
    -- root.transform.localPosition = openAniType==1 and  Vector3(self.Screen.width,0,0) or  Vector3(0,0,0);

    local obj = ResMgr:LordBySyn("UI/" .. self.name, root)
    -- Debug.LogErrorEx("创建一个预制体----------------------------"..obj.name)
    local Ing = GameObject.Instantiate(obj)
    Ing.transform.parent = root.transform
    Ing.transform.localScale = Vector3(1, 1, 1);

    Ing.transform.localPosition = Vector3(0, 0, 0);


    self.Container = Ing:GetComponent('UIPanel')
    self.Root = root



    function OnDestroy()
        self.Container = nil
        self.Root = nil
        Debug.log("销毁界面" .. self.name)
        if not self.OnDestroy or type(self.OnDestroy) ~= "function" then
            return
        end
        self:OnDestroy()
    end

    BehaviourEvent:Bind("OnDestroy", root, OnDestroy) --绑定销毁事件

    --Debug.LogErrorEx("界面创建完毕 "..self.name)
end
